package com.kaung.servlet;

import com.kaung.pojo.Person;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.nio.charset.Charset;

public class SessionDemo01  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决中文乱码问题
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        //Chrome里加这个话才能不显示为乱码
        resp.setContentType("text/html;Charset=utf-8");




        //得到session
        HttpSession session = req.getSession();

        //给session中存东西
        //session.setAttribute("name","清姜");
        session.setAttribute("name",new Person("清姜",1));

        //获取session中的ID
        String sessionId = session.getId();

        //判断session是不是新创建的
        if(session.isNew()){
            resp.getWriter().write("session在服务器中创建成功，ID:"+sessionId);

        }else {
            resp.getWriter().write("session在服务器中已经创建好了！，ID:"+sessionId);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
